package com.rock.code.common.comm;

import com.rock.code.common.exceptions.SystemException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * The type Comm date.
 *
 * @author kongqiang
 * @date 2016.10.07
 */
public class CommDate {

    public static final String YMD_L = "yyyy-MM-dd";
    public static final String YMD_S = "yyyyMMdd";
    public static final String YMDHMS_L = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMS_S = "yyyyMMddHHmmss";

    /**
     * Gets start time.
     *
     * @param startTime the start time
     * @return the start time
     * @throws Exception the exception
     * @author kongqiang
     * @date 2016.10.07
     */
    public static Date getStartTime(String startTime) throws Exception {
        startTime = startTime + " 00:00:00";
        return strToDate(startTime, YMDHMS_L);
    }

    /**
     * Gets end time.
     *
     * @param endTime the end time
     * @return the end time
     * @throws Exception the exception
     * @author kongqiang
     * @date 2016.10.07
     */
    public static Date getEndTime(String endTime) throws Exception {
        endTime = endTime + " 23:59:59";
        return strToDate(endTime, YMDHMS_L);
    }

    /**
     * Str to date.
     *
     * @param date   the date
     * @param format the format
     * @return the date
     * @throws Exception the exception
     * @author kongqiang
     * @date 2016.10.07
     */
    public static Date strToDate(String date, String format) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(date.replaceAll("/", "-"));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    /**
     * Date to str.
     *
     * @param date   the date
     * @param format the format
     * @return the string
     * @author kongqiang
     * @date 2016.10.07
     */
    public static String dateToStr(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * Calculate age.
     *
     * @param birthday the birthday
     * @return the int
     * @author kongqiang
     * @date 2016.10.07
     */
    public static int calculateAge(Date birthday) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthday)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow == monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow > monthBirth
                age--;
            }
        }
        return age;
    }
}
